<template>
  <div class="my-viewer" v-if="isShow">
    <div class="my-viewer__mask"></div>
    <div class="my-viewer__close" @click="close"><i class="el-icon-close"></i></div>
    <div class="my-viewer__canvas" @click="close">
      <div style="width: max-content;margin: 0 auto;" @click.stop="onStop">
        <video v-if="video" class="my-viewer__video" controls :src="video">
          <source :src="video" type="video/ogg">
          <source :src="video" type="video/mp4">
          <source :src="video" type="video/webm">
        </video>
        <img v-for="item in imageList" :src="item" class="my-viewer__img">
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    data() {
      return {
        isShow: false,
        imageList: [],
        video: ''
      }
    },
    methods: {
      onStop(){},
      open(imgs = [], video) {
        if (video) {
          this.video = this.getImgUrl(video)
        }
        let list = []
        for (let i in imgs) {
          list.push(this.getImgUrl(imgs[i]))
        }
        this.imageList = list
        document.querySelector('body').setAttribute('style', 'overflow: hidden')
        this.isShow = true
      },
      close() {
        this.imageList = []
        this.video = ''
        document.body.removeAttribute('style')
        this.isShow = false
      },
      getImgUrl(uri = '') {
        if (uri) {
          if (uri.substr(0, 4) === 'http' || uri.substr(0, 4) === 'blob') {
            return uri
          } else {
            return process.env.VUE_APP_BASE_IMAGE_URL + uri
          }
        }
        return ''
      }
    },
    mounted() {
    }
  }
</script>

<style scoped lang="scss">
  .my-viewer {
    position: fixed;
    top: 0;
    right: 0;
    bottom: 0;
    left: 0;
    z-index: 2001;

    .my-viewer__mask {
      position: absolute;
      width: 100%;
      height: 100%;
      top: 0;
      left: 0;
      opacity: 0.5;
      background: #000;
      z-index: -1;
    }

    .my-viewer__close {
      top: 40px;
      right: 40px;
      width: 40px;
      height: 40px;
      font-size: 24px;
      color: #fff;
      background-color: #606266;
      position: absolute;
      z-index: 1;
      display: -webkit-box;
      display: -ms-flexbox;
      display: flex;
      -webkit-box-align: center;
      -ms-flex-align: center;
      align-items: center;
      -webkit-box-pack: center;
      -ms-flex-pack: center;
      justify-content: center;
      border-radius: 50%;
      opacity: 0.8;
      cursor: pointer;
      -webkit-box-sizing: border-box;
      box-sizing: border-box;
      -webkit-user-select: none;
      -moz-user-select: none;
      -ms-user-select: none;
      user-select: none;
    }

    .my-viewer__canvas {
      width: 100%;
      height: 100%;
      overflow-y: scroll;

      .my-viewer__img, .my-viewer__video {
        margin: 40px auto;
        display: block;
        width: 800px;
      }

      .my-viewer__video {
        max-width: 800px;
        width: 100%;
      }
    }
  }
</style>
